'''
Created on Aug 11, 2012

@author: talitalopes
'''
from django import forms
from uidpapp import models
from collectionsapp.models import Collection

def collections_selector():
    collections = Collection.objects.all().order_by("name")
    all_collections = []
    for c in collections:
        all_collections.append((c.id, c.name))
     
    return tuple(all_collections)

def uidps_selector(collection_id):
    collection= Collection.objects.get(pk=collection_id)
    uidps = models.UIDP.objects.filter(collections=collection).order_by("name")
    
    all_uidps = []
    for u in uidps:
        all_uidps.append((u.id, u.name))
     
    return tuple(all_uidps)

def rel_type_selector():
    types = models.RelationshipType.objects.all().order_by("name")
    all_types = []
    for t in types:
        all_types.append((t.id, t.name))
        
    return tuple(all_types)

def action_type_selector():
    types = models.Problem.objects.distinct("action").order_by("action")
    all_types = []
    for t in types:
        all_types.append((t.id, t.action))
        
    return tuple(all_types)

def objects_type_selector():
    types = models.Problem.objects.distinct("object").order_by("object")
    all_types = []
    for t in types:
        all_types.append((t.id, t.object))
        
    return tuple(all_types)


def complement_type_selector():
    types = models.Problem.objects.distinct("complement").order_by("complement")
    all_types = []
    for t in types:
        all_types.append((t.id, t.complement))
        
    return tuple(all_types)

class UIDPForm(forms.Form):
    author = forms.CharField(max_length=200, label="Author", required=False)
    name = forms.CharField(max_length=200, label="Name")
    problem_action = forms.ChoiceField(action_type_selector(), label="Collection")
    problem_object = forms.ChoiceField(objects_type_selector(), label="Collection")
    problem_complement = forms.ChoiceField(complement_type_selector(), label="Collection", required=False)
    #problem = forms.CharField(required=False, label="Problem", widget=forms.Textarea)
    context = forms.CharField(required=False, label="Context", widget=forms.Textarea)
    rationale = forms.CharField(required=False, label="Rationale", widget=forms.Textarea)
    #literature = forms.CharField(max_length=100, label="Literature")
    
    def save(self, cd):
        uidp = models.UIDP(name = cd["name"],
                          author = cd["author"],
                          rationale = cd["rationale"])
        uidp.save()
        
        action = models.Problem.objects.get(pk=cd["problem_action"]).action
        object = models.Problem.objects.get(pk=cd["problem_object"]).object
        complement = models.Problem.objects.get(pk=cd["problem_complement"]).complement
        
        
        problem = models.Problem.objects.filter(action=action).all()
        
        if (problem):
            problem = problem.filter(object=object)
        
            if (problem):
                problem = models.Problem.objects.filter(action=action).filter(object=object).filter(complement=complement)
            
        
        if not problem:
            problem = models.Problem(action = action,
                              object = object,
                              complement = complement)
            problem.save()
        
        problem_context = models.ProblemContext(context = cd["context"],
                                                problem = problem,
                                                UIDP = uidp)
        problem_context.save()
                            
        return uidp

class ImageExampleForm(forms.Form):
    #collection = forms.ChoiceField(collections_selector(), label="Collection")
    name = forms.CharField(max_length=200, label="App name", required=True)
    image_link = forms.CharField(max_length=200, label="Example Reference", required=False)
    parent_link = forms.CharField(max_length=200, label="Parent Reference", required=False)
    target_version = forms.CharField(max_length=15, label="Platform Version", required=False)
    description = forms.CharField(required=False, label="Description", widget=forms.Textarea)

    def save(self, cd, uidp_id, collection_id):
        image = models.ImageExample(name = cd["name"],
                              image_link = cd["image_link"],
                              parent_link = cd["parent_link"],
                              description = cd["description"], 
                              target_version = cd["target_version"],
                              collection = Collection.objects.get(pk=collection_id))
        image.save()
        
        example = models.UIDPImageExample(uidp = models.UIDP.objects.get(pk=uidp_id),
                                   image = image)
        example.save()
        
        return example
    
class ImplementationExampleForm(forms.Form):
    #collection = forms.ChoiceField(collections_selector(), label="Collection")
    title = forms.CharField(max_length=200, label="Reference title", required=False)
    link = forms.CharField(max_length=200, label="Example Reference", required=False)
    target_version = forms.CharField(max_length=15, label="Platform Version")
    description = forms.CharField(required=False, label="Description", widget=forms.Textarea)

    def save(self, cd, uidp_id, collection_id):
        example = models.UIDPImplementationExample(name = cd["title"],
                              link = cd["link"],
                              description = cd["description"], 
                              uidp = models.UIDP.objects.get(pk=uidp_id),
                              target_version = cd["target_version"],
                              collection = Collection.objects.get(pk=collection_id))
            
        example.save()
        
        return example

class SolutionForm(forms.Form):
    collection = forms.ChoiceField(collections_selector(), label="Collection")
    
    solution = forms.CharField(max_length=200, label="Solution Description", required=False, widget=forms.Textarea)
    #solution_version = forms.CharField(max_length=200, label="Version", required=False)
    target_version = forms.CharField(max_length=15, label="Platform Version")

    def save(self, cd, uidp_id, collection_id):
        solution = models.UIDPSolutions(solution = cd["solution"],
                                solution_version = 1.0,
                                target_version = cd["target_version"],
                                uidp = models.UIDP.objects.get(pk=uidp_id),
                                collection = Collection.objects.get(pk=collection_id))
        solution.save()
        
        return solution
    
class RelationshipForm(forms.Form):
    rel_type = forms.ChoiceField(rel_type_selector(), label="Type")
    description = forms.CharField(required=False, label="Details", widget=forms.Textarea)
        
    def save(self, cd, uidp_source, uidp_target, collection_id):
        relationship = models.Relationship(relationship_type = models.RelationshipType.objects.get(pk=cd["rel_type"]),
                                    description = cd["description"],
                                    source = models.UIDP.objects.get(pk=uidp_source),
                                    target = models.UIDP.objects.get(pk=uidp_target),
                                    collection = Collection.objects.get(pk=collection_id))
        relationship.save()
        return relationship
    
